package com.cn.newetv.utils;

import java.util.List;

import android.content.Context;

import com.cn.newetv.po.FavDataBaseDO;

public class SystemCommon {
	private static SystemCommon self = null;
	private SysCfgDao sysCfgDao = null;
	private FavListDao favListDao = null;
	private SystemCommon() {
		
	}
	private SystemCommon(Context context) {
		try {
			sysCfgDao = new SysCfgDao();
			sysCfgDao.openDB(context);
			favListDao = new FavListDao();
			favListDao.openDB(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static SystemCommon getInstance(Context context) {
		if(self == null)
			self = new SystemCommon(context);
		return self;
	}
	public void setVersionData() {
        try {
			String res = sysCfgDao.getByKey("version");
			String ver = ETVConstant.APP_VERSION;
			if(res == null)
				sysCfgDao.add("version", ver);
			else
				sysCfgDao.modify("version", ver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean validActived() {
		try {
			String res = sysCfgDao.getByKey("isactive");
			if("0".equals(res))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean validIsFirstPwd() {
		try {
			String res = sysCfgDao.getByKey("isfirstpwd");
			if("1".equals(res))
				return false;
			else
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public boolean validIsSavePwd() {
		try {
			String res = sysCfgDao.getByKey("issavepwd");
			if("1".equals(res))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public void saveFirstPwd() {
		try {
			String res = sysCfgDao.getByKey("isfirstpwd");
			if(res == null)
				sysCfgDao.add("isfirstpwd", "1");
			else
				sysCfgDao.modify("isfirstpwd", "1");
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void saveIsSavePwd(boolean issave) {
		try {
			String res = sysCfgDao.getByKey("issavepwd");
			if(res == null)
				sysCfgDao.add("issavepwd", issave?"1":"0");
			else
				sysCfgDao.modify("issavepwd", issave?"1":"0");
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void saveParentPwd(String parentpwd) {
		try {
			String res = sysCfgDao.getByKey("parentpwd");
			if(res == null)
				sysCfgDao.add("parentpwd", parentpwd);
			else
				sysCfgDao.modify("parentpwd", parentpwd);
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getParentPwd() {
		return sysCfgDao.getByKey("parentpwd");
	}
	
	public List<FavDataBaseDO> getFavList() {
		return favListDao.findAll();
	}
	
	public void addBookMark(FavDataBaseDO favDataBaseDO) {
		favListDao.insert(favDataBaseDO);
	}
	
	public void deleteBookMark(Integer id) {
		favListDao.deleteById(id);
	}
}
